/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.search.service.impl;

import com.kmcsoft.goldshop.model.master.Booth;
import com.kmcsoft.goldshop.model.master.DiamondColor;
import com.kmcsoft.goldshop.model.master.DiamondGroup;
import com.kmcsoft.goldshop.model.master.DiamondPure;
import com.kmcsoft.goldshop.model.master.Goldsmith;
import com.kmcsoft.goldshop.model.master.JewelryStyle;
import com.kmcsoft.goldshop.model.master.Product;
import com.kmcsoft.goldshop.model.master.ProductDiamond;
import com.kmcsoft.goldshop.model.master.ProductGold;
import com.kmcsoft.goldshop.model.master.ProductJewelry;
import com.kmcsoft.goldshop.model.master.ProductPremiumJewelry;
import com.kmcsoft.goldshop.model.master.Shop;
import com.kmcsoft.goldshop.search.service.SearchService;
import com.kmcsoft.master.defaultservice.DefaultGenericService;
import com.kmcsoft.utils.i18n;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author kmcsoft02
 */
public class JdoSearchService extends DefaultGenericService implements SearchService {

    //<editor-fold defaultstate="collapsed" desc="getJewelryList">
    public Collection<HashMap<String, String>> getJewelryList(Shop shop, Booth booth, JewelryStyle jewelryStyle, ProductGold productGold, Goldsmith goldsmith,
            String totalWeightCondition, double totalWeightAmount, String gemWeightCondition,
            double gemWeightAmount, String netWeightCondition, double netWeightAmount,
            String wageCostCondition, double wageCostAmount, String wagePriceCondition,
            double wagePriceAmount, String gemPriceCondition, double gemPriceAmount) {

        Collection<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>();
        String filters = "";
        String parameters = "";
        Map<String, Object> values = new HashMap<String, Object>();
        //filter by from date
        if (null != shop) {
            parameters += "com.kmcsoft.goldshop.model.master.Shop pShop, ";
            filters += " importJewelryDetail.order.shop == pShop && ";
            values.put("pShop", shop);
        }

        if (null != booth) {
            parameters += "com.kmcsoft.goldshop.model.master.Booth pBooth, ";
            filters += " importJewelryDetail.order.booth == pBooth && ";
            values.put("pBooth", booth);
        }
        if (null != productGold) {
            parameters += "com.kmcsoft.goldshop.model.master.ProductGold pProductGold, ";
            filters += " productGold == pProductGold && ";
            values.put("pProductGold", productGold);
        }
        if (null != goldsmith) {
            parameters += "com.kmcsoft.goldshop.model.master.Goldsmith pSmith, ";
            filters += " goldsmith == pSmith && ";
            values.put("pSmith", goldsmith);
        }
        if (null != jewelryStyle) {
            parameters += "com.kmcsoft.goldshop.model.master.JewelryStyle pStyle, ";
            filters += " style == pStyle && ";
            values.put("pStyle", jewelryStyle);
        }
        if (null != totalWeightCondition) {
            parameters += "Double pTotalWeight, ";
            filters += " totalWeight " + totalWeightCondition + " == pTotalWeight && ";
            values.put("pTotalWeight", totalWeightAmount);
        }
        if (null != gemWeightCondition) {
            parameters += "Double pGemWeight, ";
            filters += " gemWeight " + gemWeightCondition + " == pGemWeight && ";
            values.put("pGemWeight", gemWeightAmount);
        }
        if (null != netWeightCondition) {
            parameters += "Double pNetWeight, ";
            filters += " netWeight " + netWeightCondition + " == pNetWeight && ";
            values.put("pNetWeight", netWeightAmount);
        }
        if (null != wageCostCondition) {
            parameters += "Double pWageCost, ";
            filters += " wageCost " + wageCostCondition + " == pWageCost && ";
            values.put("pWageCost", wageCostAmount);
        }
        if (null != gemPriceCondition) {
            parameters += "Double pGemPrice, ";
            filters += " gemPrice " + gemPriceCondition + " == pGemPrice && ";
            values.put("pGemPrice", gemPriceAmount);
        }

        parameters += "Integer pType, ";
        filters += " productType == pType && ";
        values.put("pType", ProductJewelry.TYPE_JEWELRY);

        //filter by end date


        if (parameters.length() > 0) {
            parameters = parameters.substring(0, parameters.lastIndexOf(","));
            filters = filters.substring(0, filters.lastIndexOf("&&"));
        }

        getPersistenceManager().getFetchPlan().addGroup("detach_ProductJewelry_All");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Order");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Shop");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Booth");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Collection<ProductJewelry> jewelrys = getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductJewelry.class, filters, parameters, values));
        HashMap<String, String> hm = new HashMap<String, String>();
        for (ProductJewelry jewelry : jewelrys) {
            String desc = "";
            desc += jewelry.getImportJewelryDetail().getOrder().getShop().getName() + ", ";
            desc += jewelry.getImportJewelryDetail().getOrder().getBooth().getName() + ", ";
            desc += i18n.l("shop.search.productType.jevelry") + ", ";
            desc += jewelry.getProductGold().getName() + ", ";
            desc += jewelry.getStyle().getName() + ", ";

            hm.put("picture", jewelry.getPicture());
            hm.put("desc", desc);
            hm.put("barCode", jewelry.getBarcode());
            maps.add(hm);
        }
        return maps;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getDiamondList">
    public Collection<HashMap<String, String>> getDiamondList(Shop shop, Booth booth, DiamondGroup diamondGroup, DiamondColor diamondColor,
        DiamondPure diamondPure, String gemWeightCondition, double gemWeightAmount) {
        Collection<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>();
        String filters = "";
        String parameters = "";
        Map<String, Object> values = new HashMap<String, Object>();
        //filter by from date
        if (null != shop) {
            parameters += "com.kmcsoft.goldshop.model.master.Shop pShop, ";
            filters += " importJewelryDetail.order.shop == pShop && ";
            values.put("pShop", shop);
        }

        if (null != booth) {
            parameters += "com.kmcsoft.goldshop.model.master.Booth pBooth, ";
            filters += " importJewelryDetail.order.booth == pBooth && ";
            values.put("pBooth", booth);
        }

        if (null != diamondColor) {
            parameters += "com.kmcsoft.goldshop.model.master.DiamondColor pColor, ";
            filters += " diamondColor == pColor && ";
            values.put("pColor", diamondColor);
        }

        if (null != diamondGroup) {
            parameters += "com.kmcsoft.goldshop.model.master.DiamondGroup pGroup, ";
            filters += " diamondGroup == pGroup && ";
            values.put("pGroup", diamondGroup);
        }
        if (null != diamondPure) {
            parameters += "com.kmcsoft.goldshop.model.master.DiamondPure pPure, ";
            filters += " diamondPure == pPure && ";
            values.put("pPure", diamondPure);
        }
        if (null != gemWeightCondition) {
            parameters += "Double pGemWeight, ";
            filters += " gemWeight " + gemWeightCondition + " == pGemWeight && ";
            values.put("pGemWeight", gemWeightAmount);
        }

        parameters += "Integer pType, ";
        filters += " productType == pType && ";
        values.put("pType", Product.TYPE_DIAMOND);

        if (parameters.length() > 0) {
            parameters = parameters.substring(0, parameters.lastIndexOf(","));
            filters = filters.substring(0, filters.lastIndexOf("&&"));
        }
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_ImportJewelryDetail");
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_DiamondGroup");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Order");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Shop");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Booth");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Collection<ProductDiamond> diamonds = getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductDiamond.class, filters, parameters, values));
        HashMap<String, String> hm = new HashMap<String, String>();
        for (ProductDiamond jewelry : diamonds) {
            String desc = "";
            desc += jewelry.getImportJewelryDetail().getOrder().getShop().getName() + ", ";
            desc += jewelry.getImportJewelryDetail().getOrder().getBooth().getName() + ", ";
            desc += i18n.l("shop.search.productType.diamond") + ", ";
            desc += jewelry.getDiamondGroup().getName() + ", ";

            hm.put("picture", jewelry.getPicture());
            hm.put("desc", desc);
            hm.put("barCode", jewelry.getBarcode());
            maps.add(hm);
        }
        return maps;
    }
    //</editor-fold>

    public Collection<HashMap<String, String>> getPremiumList(Shop shop, Booth booth, JewelryStyle jewelryStyle, String gemWeightCondition, double gemWeightAmount, String gemPriceCondition, double gemPriceAmount) {
        Collection<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>();
        String filters = "";
        String parameters = "";
        Map<String, Object> values = new HashMap<String, Object>();
        //filter by from date 
        if (null != shop) {
            parameters += "com.kmcsoft.goldshop.model.master.Shop pShop, ";
            filters += " importJewelryDetail.order.shop == pShop && ";
            values.put("pShop", shop);
        }

        if (null != booth) {
            parameters += "com.kmcsoft.goldshop.model.master.Booth pBooth, ";
            filters += " importJewelryDetail.order.booth == pBooth && ";
            values.put("pBooth", booth);
        }

        if (null != jewelryStyle) {
            parameters += "com.kmcsoft.goldshop.model.master.JewelryStyle pStyle, ";
            filters += " style == pStyle && ";
            values.put("pStyle", jewelryStyle);
        }
        if (null != gemWeightCondition) {
            parameters += "Double pGemWeight, ";
            filters += " gemWeight " + gemWeightCondition + " == pGemWeight && ";
            values.put("pGemWeight", gemWeightAmount);
        }
        if (null != gemPriceCondition) {
            parameters += "Double pGemPrice, ";
            filters += " gemPrice " + gemPriceCondition + " == pGemPrice && ";
            values.put("pGemPrice", gemPriceAmount);
        }

        parameters += "Integer pType, ";
        filters += " productType == pType && ";
        values.put("pType", Product.TYPE_PREMIUMJEWELRY);

        if (parameters.length() > 0) {
            parameters = parameters.substring(0, parameters.lastIndexOf(","));
            filters = filters.substring(0, filters.lastIndexOf("&&"));
        }
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductPremiumJewelry_Detail");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Order");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Shop");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Booth");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Collection<ProductPremiumJewelry> premiumJewelrys = getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductPremiumJewelry.class, filters, parameters, values));
        HashMap<String, String> hm = new HashMap<String, String>();
        for (ProductPremiumJewelry jewelry : premiumJewelrys) {
            String desc = "";
            desc += jewelry.getImportJewelryDetail().getOrder().getShop().getName() + ", ";
            desc += jewelry.getImportJewelryDetail().getOrder().getBooth().getName() + ", ";
            desc += i18n.l("shop.search.productType.premium") + ", ";
            desc += jewelry.getStyle().getName() + ", ";

            hm.put("picture", jewelry.getPicture());
            hm.put("desc", desc);
            hm.put("barCode", jewelry.getBarcode());
            maps.add(hm);
        }
        return maps;
    }
}
